<template>
  <div class="energy-system-container">
    <div class="section">
      <h2 class="section-title">计算参数</h2>
      <div class="grid">
        <div class="card">
          <div class="card-content-wrapper">
            <!-- 评估方法选择 -->
            <div class="param-group">
              <label class="card-label">评估方法</label>
              <el-radio-group
                v-model="evaluationMethod"
                @change="handleEvaluationMethodChange"
                class="radio-group"
              >
                <el-radio :label="1" class="radio-item">蒙特卡洛模拟次数</el-radio>
                <el-radio :label="2" class="radio-item">状态枚举阶数</el-radio>
              </el-radio-group>

              <!-- 参数输入 -->
              <div v-if="showInputField" class="param-group mt-20">
                <div class="input-group">
                  <el-input
                    v-model.number="inputValue"
                    type="number"
                    class="input phase-period-input"
                    :min="minInputValue"
                    :placeholder="inputPlaceholder"
                  ></el-input>
                  <span class="unit">{{ inputUnit }}</span>
                </div>
              </div>

              <!-- 提交按钮 -->
              <button class="distribution-btn emission-submit-btn" @click="submitEvaluationParams">
                <span class="btn-text">提交评估参数</span>

              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

// 状态：评估方法
const evaluationMethod = ref(1)
const inputValue = ref(1000)

// 显示输入框条件
const showInputField = computed(() => evaluationMethod.value === 1 || evaluationMethod.value === 2)

// 输入提示占位
const inputPlaceholder = computed(() =>
  evaluationMethod.value === 1 ? '输入模拟次数' : '输入枚举阶数'
)

// 单位
const inputUnit = computed(() => (evaluationMethod.value === 1 ? '次' : '阶'))

// 最小值限制（避免用户输入过小无效值）
const minInputValue = computed(() => (evaluationMethod.value === 1 ? 100 : 1))

// 评估方式变更时自动设置默认值
const handleEvaluationMethodChange = (value: number) => {
  inputValue.value = value === 1 ? 1000 : 3
}

// 提交函数
const submitEvaluationParams = async () => {
  try {
    const method = evaluationMethod.value;
    const num = inputValue.value;
    const response = await axios.put(`http://localhost:8080/reliability/insert/${method}/${num}`)
    if(response.data.code === 0){
      ElMessage.success('设备参数提交成功');
    }else{
      ElMessage.success(response.data.message);
    }
  } catch (err) {
    console.error(err)
    ElMessage.error('提交失败，请检查网络或接口')
  }
}
</script>

<style scoped>
.submit-emission-container {
  text-align: center;
  margin-top: 24px;
}

.emission-submit-btn {
  background-color: #7c3aed; /* 绿色 */
  padding: 12px 24px;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  box-shadow: 0 4px 10px rgba(22, 163, 74, 0.2);
}

.emission-submit-btn:hover {
  background-color: #7c3aed;
  box-shadow: 0 6px 16px rgba(22, 163, 74, 0.3);
}
.energy-system-container {
  background-color: #f8fafc;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  border-radius: 14px;
  padding: 36px;
  max-width: 1200px;
  margin: 0 auto;
  font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
  color: #1e293b;
}

.section-title {
  color: #4c1d95;
  margin-bottom: 24px;
  font-size: 24px;
  font-weight: 700;
  padding-bottom: 12px;
  border-bottom: 2px solid #7c3aed;
}

.section {
  background-color: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 28px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

.card {
  background-color: white;
  border-radius: 8px;
  border-left: 4px solid #7c3aed;
  transition: transform 0.2s, box-shadow 0.2s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  padding: 24px;
}

.card-content-wrapper {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.param-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.input {
  padding: 8px 12px;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  font-size: 14px;
  background: white;
  color: #1e293b;
  transition: border-color 0.3s;
}

.input.small {
  width: 80px;
}

.input.phase-period-input {
  width: 120px;
}

.input:focus {
  outline: none;
  border-color: #7c3aed;
  box-shadow: 0 0 0 2px rgba(124, 58, 237, 0.1);
}

.unit {
  font-weight: 500;
  color: #64748b;
  font-size: 13px;
  white-space: nowrap;
}

.error-message {
  color: #ef4444;
  font-size: 12px;
  margin-left: 8px;
}

.radio-group {
  display: flex;
  gap: 16px;
}

.radio-item {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #64748b;
  font-size: 14px;
  padding: 8px 16px;
  border-radius: 6px;
  transition: background-color 0.3s;
}

.radio-item:hover {
  background-color: #f8fafc;
}

.el-radio.is-checked .radio-item {
  color: #7c3aed;
  font-weight: 500;
  background-color: #f8fafc;
}

.mt-20 {
  margin-top: 20px;
}
</style>
